package com.yc.think.brain;

/**
 * author: yc
 * description: ${description}
 * date: 2021/7/16 16:06
 */
public class Learn {

    // 将字符串转换成二进制字符串，以空格相隔
    public String StrToBinstr(String str) {
        char[] strChar = str.toCharArray();
        String result = "";
        for (int i = 0; i < strChar.length; i++) {
            result += Integer.toBinaryString(strChar[i]) + " ";
        }
        return result;
    }

    public String BinstrToStr(String binStr) {
        String[] tempStr = StrToStrArray(binStr);
        char[] tempChar = new char[tempStr.length];
        for (int i = 0; i < tempStr.length; i++) {
            tempChar[i] = BinstrToChar(tempStr[i]);
        }
        return String.valueOf(tempChar);
    }

    // 将初始二进制字符串转换成字符串数组，以空格相隔
    private String[] StrToStrArray(String str) {
        return str.split(" ");
    }

    // 将二进制字符串转换为char
    private char BinstrToChar(String binStr) {
        int[] temp = BinstrToIntArray(binStr);
        int sum = 0;
        for (int i = 0; i < temp.length; i++) {
            sum += temp[temp.length - 1 - i] << i;
        }
        return (char) sum;
    }

    // 将二进制字符串转换成int数组
    private int[] BinstrToIntArray(String binStr) {
        char[] temp = binStr.toCharArray();
        int[] result = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            result[i] = temp[i] - 48;
        }
        return result;
    }

    public void m1() {
        String s1 = "hello";
        System.out.println(StrToBinstr(s1));
    }

    public void m2() {
        String c1 = "1101000 1100101 1101100 1101100 1101111";
        System.out.println(BinstrToStr(c1));
    }

    //随机产生一个二进制字符串，0，1，空格
    public void m3() {
        //输入一个节点，
    }



    public static void main(String[] args) {
        Learn learn = new Learn();
//        learn.m1();
        learn.m2();

    }
}
